package com.hesicare.health.worker;

import com.hesicare.adapter.entity.HisPatient;
import com.hesicare.health.entity.SysDept;
import com.hesicare.health.entity.SysPatient;
import com.hesicare.health.entity.SysPatientRecord;
import com.hesicare.health.entity.SysUser;
import com.hesicare.health.service.ISysDeptService;
import com.hesicare.health.service.ISysPatientRecordService;
import com.hesicare.health.service.ISysPatientService;
import com.hesicare.health.service.ISysUserService;
import com.hesicare.untils.StringUtil.StringUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Date;

import static com.hesicare.enums.deptEnum.DEFAULTDEPT;
import static com.hesicare.enums.patientEnum.*;
import static com.hesicare.untils.exceptionUtils.exceptionutil.errorTrackSpace;

@Component
public class DoPatient {

    @Autowired
    private ISysPatientService ISysPatientService;

    @Autowired
    private ISysUserService ISysUserService;

    @Autowired
    private ISysDeptService ISysDeptService;

    @Autowired
    private ISysPatientRecordService ISysPatientRecordService;

    protected final static Logger LOGGER = LoggerFactory.getLogger(DoPatient.class);

    public synchronized boolean doPatient(HisPatient hisPatient) {

        /**
         * 根据实际情况 选择性注释 住院状态、住院号等信息判断代码
         * */
        if (!checkPatient(hisPatient)) {
            return false;
        }
        SysPatient sysPatient = null;
        if (StringUtil.isBlank(hisPatient.getCardNumber())) {
            sysPatient = ISysPatientService.selectSysPatientByHisPatientId(hisPatient.getPatientId());
        } else {
            sysPatient = ISysPatientService.selectSysPatientByHospitalizedNum(hisPatient.getCardNumber());
        }
        if (sysPatient == null && !OUTHOSPITAL.getType().equals(hisPatient.getHospitalState())) {
            try {
                sysPatient = new SysPatient();
                this.saveDept(hisPatient, sysPatient);
                if (sysPatient.getDeptId() == null) {
                    return false;
                }
                this.transform(hisPatient, sysPatient);
                this.createPatientInfo(sysPatient);
                ISysPatientService.insertSysPatient(sysPatient);
                LOGGER.info("新增患者" + sysPatient.toString());
                return true;
            } catch (Exception e) {
                LOGGER.error("新增患者错误", errorTrackSpace(e));
                return false;
            }
        } else if (sysPatient != null) {
            if (!isChange(hisPatient, sysPatient)) {
                return false;
            }
            try {
                this.saveDept(hisPatient, sysPatient);
                if (sysPatient.getDeptId() == null) {
                    sysPatient.setDeptId(DEFAULTDEPT.getType());
                }
                this.transform(hisPatient, sysPatient);
                sysPatient.setHospitalizedType(hisPatient.getHospitalState());
                this.updatePatientInfo(sysPatient);
                ISysPatientService.updateSysPatient(sysPatient);
                LOGGER.info("更新患者" + sysPatient);
                return true;
            } catch (Exception e) {
                LOGGER.error("更新患者错误" , errorTrackSpace(e));
                return false;
            }
        }
        return false;


    }

    public boolean checkPatient(HisPatient hisPatient) {
        if (hisPatient == null) {
            return false;
        }
        if (StringUtil.isBlank(hisPatient.getPatientId())) {
            return false;
        }
        if (StringUtil.isBlank(hisPatient.getHospitalState())) {
            return false;
        }
        //if (StringUtil.isBlank(hisPatient.getCardNumber())) {
        //    return false;
        // }
        return true;
    }

    public SysPatient transform(HisPatient hisPatient, SysPatient sysPatient) {
        sysPatient.setBedNum(hisPatient.getBedNumber());
        sysPatient.setHospitalizedDate(hisPatient.getHospitalDate());
        sysPatient.setHisPatientId(hisPatient.getPatientId());
        sysPatient.setHospitalizedType(hisPatient.getHospitalState());
        sysPatient.setPatientName(hisPatient.getName());
        sysPatient.setPatientId(sysPatient.getPatientId());
        sysPatient.setWristbandCode(hisPatient.getWistBandCode());
        sysPatient.setBirth(hisPatient.getBirth());
        sysPatient.setAddress(hisPatient.getAddress());
        sysPatient.setSex(hisPatient.getGender());
        SysUser sysUser = ISysUserService.getUserByLoginName(hisPatient.getDoctorId());
        if (sysUser != null) {
            sysPatient.setDoctorId(sysUser.getUserId());
        }
        sysPatient.setDoctorName(hisPatient.getDoctorName());
        sysPatient.setRemark(hisPatient.getExt1());
        sysPatient.setLoginName(hisPatient.getHospitalTimes());
        sysPatient.setHospitalizedNum(hisPatient.getCardNumber());
        return sysPatient;

    }

    public void saveDept(HisPatient hisPatient, SysPatient sysPatient) {
        SysDept sysDept = this.getSysDept(hisPatient);
        if (sysDept == null) {
            sysPatient.setDeptId(null);
        } else {
            sysPatient.setDeptId(sysDept.getDept_id());
        }

    }

    public SysDept getSysDept(HisPatient hisPatient) {
        SysDept sysDept = null;
        if (StringUtil.isNotBlank(hisPatient.getDepartment()) && StringUtil.isNotBlank(hisPatient.getWardArea())) {
            sysDept = ISysDeptService.getSysDeptByHisCode(hisPatient.getWardArea());
        }
        if (StringUtil.isNotBlank(hisPatient.getDepartment()) && StringUtil.isBlank(hisPatient.getWardArea())) {
            sysDept = ISysDeptService.getSysDeptByHisCode(hisPatient.getDepartment());
        }
        if (StringUtil.isBlank(hisPatient.getDepartment()) && StringUtil.isNotBlank(hisPatient.getWardArea())) {
            sysDept = ISysDeptService.getSysDeptByHisCode(hisPatient.getWardArea());
        }
        return sysDept;
    }

    public SysPatient createPatientInfo(SysPatient sysPatient) {
        sysPatient.setCreateTime(new Date());
        sysPatient.setCreateBy(CREATEBY.getType());
        sysPatient.setStatus("0");
        return sysPatient;
    }

    public SysPatient updatePatientInfo(SysPatient sysPatient) {
        sysPatient.setUpdateBy(CREATEBY.getType());
        sysPatient.setUpdateTime(new Date());
        if (OUTHOSPITAL.getType().equals(sysPatient.getHospitalizedType())) {
            sysPatient.setLeaveDate(new Date());
        }
        return sysPatient;
    }

    public boolean isChange(HisPatient hisPatient, SysPatient sysPatient) {
        SysDept sysDept = this.getSysDept(hisPatient);
        if (sysDept == null) {
            if (sysPatient.getDeptId() != DEFAULTDEPT.getType()) {
                SysPatientRecord patientRecord = new SysPatientRecord();
                BeanUtils.copyProperties(sysPatient, patientRecord);
                patientRecord.setHospitalizedType(TRANSFERHOSPITAL.getType());
                patientRecord.setLeaveTime(new Date());
                patientRecord.setTargetDeptId(240L);
                ISysPatientRecordService.insertPatientRecord(patientRecord);
                return true;
            }
        }
        if ((sysDept != null && (!sysDept.getDept_id().equals(sysPatient.getDeptId())))) {
            SysPatientRecord patientRecord = new SysPatientRecord();
            BeanUtils.copyProperties(sysPatient, patientRecord);
            patientRecord.setHospitalizedType(TRANSFERHOSPITAL.getType());
            patientRecord.setLeaveTime(new Date());
            patientRecord.setTargetDeptId(Long.valueOf(sysDept.getDept_id()));
            ISysPatientRecordService.insertPatientRecord(patientRecord);
            return true;
        }
        if (!hisPatient.getHospitalState().equals(sysPatient.getHospitalizedType())) {
            SysPatientRecord sysPatientRecord = new SysPatientRecord();
            BeanUtils.copyProperties(sysPatient, sysPatientRecord);
            ISysPatientRecordService.insertPatientRecord(sysPatientRecord);
            return true;
        }
        if (!hisPatient.getBedNumber().equals(sysPatient.getBedNum())) {
            return true;
        }
        if (StringUtil.isNotBlank(hisPatient.getCardNumber()) && !hisPatient.getCardNumber().equals(sysPatient.getHospitalizedNum())) {
            return true;
        }
        if (!hisPatient.getWistBandCode().equals(sysPatient.getWristbandCode())) {
            return true;
        }
        return false;
    }
}
